package by.yakusekmaxim.bookgid.webapp.page.home.myLibrary;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;

import javax.inject.Inject;
import javax.persistence.metamodel.SingularAttribute;

import org.apache.wicket.AttributeModifier;
import org.apache.wicket.extensions.markup.html.repeater.data.sort.SortOrder;
import org.apache.wicket.extensions.markup.html.repeater.util.SortableDataProvider;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.navigation.paging.PagingNavigator;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.markup.repeater.data.DataView;
import org.apache.wicket.markup.repeater.data.IDataProvider;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.IModel;

import by.yakusekmaxim.bookgid.dataaccess.util.Filter;
import by.yakusekmaxim.bookgid.datamodel.Book;
import by.yakusekmaxim.bookgid.datamodel.UserAccount;
import by.yakusekmaxim.bookgid.datamodel.UserAccount2Book;
import by.yakusekmaxim.bookgid.datamodel.UserAccount2Book_;
import by.yakusekmaxim.bookgid.services.UserAccount2BookService;
import by.yakusekmaxim.bookgid.webapp.app.BasicAuthenticationSession;
import by.yakusekmaxim.bookgid.webapp.page.home.book.BookDetailsPage;

public class UserAccount2BookListPanel extends Panel {

	@Inject
	UserAccount2BookService userAccount2BookService;

	UserAccount user = BasicAuthenticationSession.get().getUser();
	
	public UserAccount2BookListPanel(String id, final Filter<UserAccount2Book> filter) {
		super(id);

		IDataProvider<UserAccount2Book> bookDataProvider = new BookDataProvider(
				filter);

		DataView<UserAccount2Book> dataView = new DataView<UserAccount2Book>(
				"list", bookDataProvider, 10) {
			@Override
			protected void populateItem(Item<UserAccount2Book> item) {
				final UserAccount2Book userAccount2Book = item.getModelObject();
				final Book book = userAccount2Book.getBook();
				Label imageLabel = new Label("image", "");
				imageLabel.add(AttributeModifier.replace("src", book.getImage()
						.getUrl()));
				imageLabel.add(AttributeModifier.replace("alt", book.getImage()
						.getName()));
				imageLabel.add(AttributeModifier.replace("title", book
						.getImage().getName()));
				item.add(imageLabel);

				Link<Object> linkBook = new Link<Object>("linkBook") {
					@Override
					public void onClick() {

						setResponsePage(new BookDetailsPage(book));

					}
				};
				linkBook.add(new Label("name", book.getName()));
				item.add(linkBook);
				item.add(new Label("nameOr", book.getNameOr()));
				item.add(new Label("date", getDataFormat(book.getDate())));
				item.add(new Label("author", getAuthorName(book)));
				item.add(new Label("genre", getGenreName(book)));

			}

		};

		add(dataView);

		add(new PagingNavigator("paging", dataView));

	}

	private class BookDataProvider extends
			SortableDataProvider<UserAccount2Book, SingularAttribute<UserAccount2Book, ?>> {

		private Filter<UserAccount2Book> filter = new Filter<UserAccount2Book>();

		public BookDataProvider(Filter<UserAccount2Book> filter) {
			super();
			setSort(UserAccount2Book_.book, SortOrder.ASCENDING);
			this.filter = filter;

		}

		@Override
		public Iterator<? extends UserAccount2Book> iterator(long first, long count) {

			SingularAttribute<UserAccount2Book, ?> sortParam = getSort().getProperty();
			SortOrder propertySortOrder = getSortState().getPropertySortOrder(
					sortParam);
			boolean ascending = SortOrder.ASCENDING.equals(propertySortOrder);

			if (filter.getEnabled()) {
				return userAccount2BookService.getAllUserAccount2BookDao(sortParam,
						ascending, (int) first, (int) count, filter, user).iterator();
			}
			return userAccount2BookService.getAllUserAccount2BookDao(sortParam,
					ascending, (int) first, (int) count, user).iterator();
		}

		@Override
		public long size() {
			System.out.println(filter.getEnabled());
			if (filter.getEnabled()) {
				return userAccount2BookService.getCountByUserAccount(filter, user);
			}
			System.out.println(userAccount2BookService.getCountByUserAccount(user));
			return userAccount2BookService.getCountByUserAccount(user);
		}

		@Override
		public IModel<UserAccount2Book> model(UserAccount2Book userAccount2Book) {
			return new CompoundPropertyModel<UserAccount2Book>(userAccount2Book);
		}
	}

	private String getDataFormat(Date date) {
		if (date == null) {
			return "-";
		}
		SimpleDateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy");
		return dateFormat.format(date);
	}

	private String getAuthorName(Book book) {
		if (book.getAuthor() == null) {
			return "-";
		}
		return book.getAuthor().getName();
	}

	private String getGenreName(Book book) {
		if (book.getGenre() == null) {
			return "-";
		}
		return book.getGenre().getName();
	}

}
